<?php

function render($code, $message, $result = '') {
    if (is_array($result)) {
        $result = array_merge($result, array('site_url' => C('SITE_URL'),));
    }
    $result = set_null($result);
    echo json_encode(array(
        'code' => $code,
        'message' => $message,
        'result' => $result
    ));
    exit;
}

function set_null($data) {
    if (is_array($data)) {
        foreach ($data as $key => $val) {
            $data[$key] = set_null($val);
        }
    }
    if ((is_string($data) && $data === '') || (is_array($data) && $data === array())) {
        $data = NULL;
    }
    return $data;
}



/**
 *    获取订单状态相应的文字表述
 */
function order_status($order_status) {
    $lang_key = '订单状态错误';
    switch ($order_status) {
        case ORDER_PENDING:
            $lang_key = '待付款';
            break;
        case ORDER_SUBMITTED:
            $lang_key = '已提交';
            break;
        case ORDER_ACCEPTED:
            $lang_key = '待发货';
            break;
        case ORDER_SHIPPED:
            $lang_key = '已发货';
            break;
        case ORDER_FINISHED:
            $lang_key = '已完成';
            break;
        case ORDER_CANCELED:
            $lang_key = '已取消';
            break;
    }
    return $lang_key;
}

/**
 * 发送get请求
 * @param string $url
 * @return bool|mixed
 */
function request_get($url = '') {
    if (empty($url)) {
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    //curl_redir_exec($ch);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Errno' . curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

/**
 * 发送post请求
 * @param string $url
 * @param string $param
 * @return bool|mixed
 */
function request_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
        return false;
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
//    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Errno' . curl_error($ch);
    }
    curl_close($ch);
    return $data;
}

function real_ip() {
    static $realip = NULL;

    if ($realip !== NULL) {
        return $realip;
    }

    if (isset($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

            /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */
            foreach ($arr AS $ip) {
                $ip = trim($ip);

                if ($ip != 'unknown') {
                    $realip = $ip;

                    break;
                }
            }
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            if (isset($_SERVER['REMOTE_ADDR'])) {
                $realip = $_SERVER['REMOTE_ADDR'];
            } else {
                $realip = '0.0.0.0';
            }
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $realip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('HTTP_CLIENT_IP')) {
            $realip = getenv('HTTP_CLIENT_IP');
        } else {
            $realip = getenv('REMOTE_ADDR');
        }
    }

    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';

    return $realip;
}

function _post_to_oms($url,$port=80,$t=30,$method='get',$data=null)
{
    $info=parse_url($url);
    $fp = fsockopen($info['host'],$port,$errno,$errstr,$t);
    // 判断是否有数据
    if(isset($data) && !empty($data))
    {
        $query = json_encode($data); // 数组转url 字符串形式
//        $query = http_build_query($data); // 数组转url 字符串形式
    }else
    {
        $query=null;
    }
    // 如果用户的$url "http://www.manongjc.com/";  缺少 最后的反斜杠
    if(!isset($info['path']) || empty($info['path']))
    {
        $info['path']="/index.html";
    }
    // 判断 请求方式
    if($method=='post')
    {
        $head = "POST ".$info['path']." HTTP/1.0".PHP_EOL;
    }else
    {
        $head = "GET ".$info['path']."?".$query." HTTP/1.0".PHP_EOL;
    }

    $head .= "Host: ".$info['host'].PHP_EOL; // 请求主机地址
    $head .= "Referer: http://".$info['host'].':'.$port.$info['path'].PHP_EOL;
    if(isset($data) && !empty($data) && ($method=='post'))
    {
        $head .= "Content-type: application/x-www-form-urlencoded".PHP_EOL;
        $head .= "Content-Length: ".strlen(trim($query)).PHP_EOL;
        $head .= PHP_EOL;
        $head .= trim($query);
    }else
    {
        $head .= PHP_EOL;
    }
//        var_dump($head);exit;
    $write = fputs($fp, $head); //写入文件(可安全用于二进制文件)。 fputs() 函数是 fwrite() 函数的别名
    while (!feof($fp))
    {
        $line = fread($fp,4096);
        echo $line;
    }
}

function sync_data($url, $data_string) {
    $data_string = json_encode($data_string);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    //https
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($data_string))
    );
    ob_start();
    $res = curl_exec($ch);
    if(!$res){
        return false;
    }else{
        return json_decode($res,true);
    }
}


?>
